package com.bitinstitute.parkapp.help;

import java.io.File;
import com.bitinstitute.parkapp.R;

import android.app.Activity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;
import android.os.Bundle;

public class HelpActivity extends Activity implements ViewFactory{
	
	TextView text;
	ImageView view;
	
	int i = 0;
	
	int imageId[]={R.drawable.picture1, R.drawable.pic2, R.drawable.picture3,R.drawable.picture4,R.drawable.picture5};
	String imageNames[]={"picture1.png", "pic2.png", "picture3.png", "picture4.png","picture5.png"};
	
	
	
	ImageSwitcher imageswitch;
	
	int curindex =0;
	int downX, upX;
	
	
	/**
	 *  Called when the activity is first created. 
	 *  defines the functionality of the ImageSwitcher, to switch the pictures
	 *  of the help function from right to left and from left to right
	 */
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        
        clearApplicationData();
        
//         
//        for(int i=0; i<imageId.length; i++)
//        {
//        	File f = new File(imageNames[i]);
//        	decodeFile(f);
//        }
        
        setContentView(R.layout.help);
        
             
        imageswitch = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
        imageswitch.setFactory(this);
        imageswitch.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
        imageswitch.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));

        
        imageswitch.setImageResource(imageId[curindex]);

        imageswitch.setOnTouchListener(new View.OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent event) {
				
		        if (event.getAction() == MotionEvent.ACTION_DOWN) {
		             downX = (int) event.getX(); 
		             Log.i("event.getX()", " downX " + downX);
		             return true;
		         } 

		         else if (event.getAction() == MotionEvent.ACTION_UP) {
		             upX = (int) event.getX(); 
		             Log.i("event.getX()", " upX " + downX);
		             if (upX - downX > 100) {

		                 //curIndex  current image index in array viewed by user
		                 curindex--;
		                 if (curindex < 0) {
		                     curindex = imageId.length-1;
		                 }

		                 imageswitch.setInAnimation(AnimationUtils.loadAnimation(HelpActivity.this,R.anim.slide_in_left));
		                 imageswitch.setOutAnimation(AnimationUtils.loadAnimation(HelpActivity.this,R.anim.slide_out_right));

		                 imageswitch.setImageResource(imageId[curindex]);
		                 //GalleryActivity.this.setTitle(curIndex);
		             } 

		             else if (downX - upX > -100) {

		                 curindex++;
		                 if (curindex > 4) {
		                     curindex = 0;
		                 }

		                 imageswitch.setInAnimation(AnimationUtils.loadAnimation(HelpActivity.this,R.anim.slight_in_right));
		                 imageswitch.setOutAnimation(AnimationUtils.loadAnimation(HelpActivity.this,R.anim.slide_out_left));

		                 imageswitch.setImageResource(imageId[curindex]);
		                 //GalleryActivity.this.setTitle(curIndex);
		             }
		                 return true;
		             }
		             return false;
		         }
        });
	}



	public View makeView() {
		ImageView i = new ImageView(this);  
	    i.setScaleType(ImageView.ScaleType.FIT_CENTER);
	    return i;
	}
	

	/**
	 * clears the cache of the APP
	 */
	   public void clearApplicationData() {
	        File cache = getCacheDir();
	        File appDir = new File(cache.getParent());
	        if (appDir.exists()) {
	            String[] children = appDir.list();
	            for (String s : children) {
	                if (!s.equals("lib")) {
	                    deleteDir(new File(appDir, s));
	                    Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
	                }
	            }
	        }
	    }

	    public static boolean deleteDir(File dir) {
	        if (dir != null && dir.isDirectory()) {
	            String[] children = dir.list();
	            for (int i = 0; i < children.length; i++) {
	                boolean success = deleteDir(new File(dir, children[i]));
	                if (!success) {
	                    return false;
	                }
	            }
	        }

	        return dir.delete();
	    }


}

	
